
/*
 * 1.1 changes
 * ~fixed decryption alogrithm
 */

package btracker;

import java.util.Random;

/**
 * 
 * 
 *
 * Crypt (c) Sammy Guergachi 2010
 * encrypts and decrypts a string with simple shuffling of characters algorithm
 *
 * Method encrypt:
 * Give any string with any length as parameter
 * returns an array with [0] as the key and [1] as the encrypted password
 *
 * Method decrypt:
 * Give string key and encrypted string password
 * returns decrypted pass as string
 * 
 * @version 1.1
 * @author Sammy
 */
public class Crypt {

    static int Key[];

    public static String[] encrypt(String Pass) {

        String cryptPass[] = new String[2];
        Random rgen = new Random();
        Pass.trim();
        int Index[] = new int[Pass.length()];
        int RandIndex[] = new int[Pass.length()];
        String index[] = new String[Pass.length()];
        StringBuffer buff = new StringBuffer();
        StringBuffer buf = new StringBuffer();





        ///Shuffle Index

        for (int i = 0; i < RandIndex.length; i++) {

            Index[i] = i;
            buf.append(Index[i]);
            buf.toString();
            RandIndex[i] = i;

            System.out.print(Index[i]);


        }
        System.out.print(" Password:" + Pass);
        System.out.print("    ");


        for (int i = 0; i < RandIndex.length; i++) {

            int randomPosition = rgen.nextInt(RandIndex.length);
            int temp = RandIndex[i];
            RandIndex[i] = RandIndex[randomPosition];
            RandIndex[randomPosition] = temp;

        }
        for (int i = 0; i < RandIndex.length; i++) {

            buff.append(Integer.toString(RandIndex[i]));

        }


        cryptPass[0] = buff.toString();
        System.out.print(" Shuffled index: " + cryptPass[0]);



        ////Shuffle String
        StringBuffer sbPass = new StringBuffer();
        for (int i = 0; i < Index.length; i++) {
            sbPass.append(Pass.charAt(RandIndex[i]));
        }


        System.out.print(" Encrypted Pass: " + sbPass.toString());

        cryptPass[1] = sbPass.toString();



        return cryptPass;
    }

    public static String decrypt(String cryptPass, String cryptKey) {
        String decryptPass = null;

        Key = new int[cryptKey.length()];

        //put each cryptKey number into an element of Key[]
        for (int a = 0; a < cryptKey.length(); a++) {

            Key[a] = Integer.parseInt(Character.toString(cryptKey.charAt(a)));
        }
        int counter = 0;
       

        StringBuffer sbDecrypt = new StringBuffer();
       
        for (int a = 0; a < cryptKey.length(); a++) {
           for(int i =0; i<cryptKey.length(); i++){
               if(Key[i]== counter){
                  
                   sbDecrypt.append(cryptPass.charAt(i));
               }else{

               }
           }
           counter++;
        }

        decryptPass = sbDecrypt.toString();
        System.out.println(" Decrypted pass: " + decryptPass);
        return decryptPass;
    }
}
